home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / perl / examples / bricks < prev    next >
Encoding:
Text File  |  2000-12-27  |  5.1 KB  |  148 lines

  1. #!/usr/app/bin/perl
  2.  
  3. eval 'exec /usr/app/bin/perl  -S $0 ${1+"$@"}'
  4.     if 0; # not running under some shell
  5.  
  6. # [10/27/1999] v0.0.1 First version. 
  7.  
  8. # (c) Copyright by Tels www.bloodgate.com 1999.
  9.  
  10. # generate a brick texture for using in 3d games
  11.  
  12. # params: brick width/height, brick pattern, random light skew, border color
  13.  
  14. use Gimp qw(:auto N_);
  15. use Gimp::Fu;
  16. use Gimp::Util;
  17.  
  18. sub do_bricks {
  19.     my ($pattern,$pattern2,$bp,$clr,$bw,$brickx,$bricky,$imgw,$imgh,$skew) = @_;
  20.     my ($image, $layer, $state, $layerpat);
  21.   
  22.     $imgw = abs($imgw); $imgw = 256 if (($imgw < 32) || ($imgw > 4096));
  23.     $imgh = abs($imgh); $imgh = 256 if (($imgh < 32) || ($imgh > 4096));
  24.     #print "Creating texture $imgw"."x$imgh\n";
  25.     $image = gimp_image_new($imgw,$imgh,RGB);
  26.     # make background
  27.     gimp_patterns_set_pattern($pattern);
  28.     $layerpat = gimp_image_add_new_layer($image,0,0,0);
  29.     gimp_selection_all($image);
  30.     gimp_bucket_fill($layerpat,2,0,100,0,0,1,1);
  31.     
  32.     # make border lines
  33.     gimp_palette_set_foreground ("#ffffff"); 
  34.     $layer = gimp_image_add_new_layer($image,0,0,0);
  35.     gimp_drawable_fill($layer,3);
  36.     my $w = 1; my $h = 1; my $j = 0; my $wo = 0;
  37.     my $brickw = ($imgw / $brickx); my $brickh = ($imgh / $bricky);
  38.     gimp_undo_push_group_start($image);
  39.     while ($h < $imgh)
  40.       {
  41.       gimp_rect_select($image,0,$h,$imgw,$bw,2,0,0);
  42.       gimp_bucket_fill($layer,0,0,100,0,0,0,0);
  43.       $w = 1;
  44.       $wo = 0; $wo = ($brickw / 2) if ($j == 1);
  45.       while ($w < $imgw)
  46.         {
  47.         gimp_rect_select($image,$w+$wo,$h,$bw,$brickh+1,2,0,0);
  48.         gimp_bucket_fill($layer,0,0,100,0,0,1,1);
  49. #        print "$h $w\n";
  50.         $w += $brickw;
  51.         }
  52.       $j = 1 - $j;
  53.       $h += $brickh;
  54.       }
  55.     gimp_undo_push_group_end($image);
  56.     # make a copy of it for bump mapping
  57.     gimp_selection_all($image);
  58.     $layer->edit_copy(); 
  59.     $border = gimp_image_add_new_layer($image,0,0,0);
  60.     #print "$border\n";
  61.     $border->edit_paste(0);
  62.     gimp_floating_sel_anchor(gimp_image_floating_selection($image));
  63.     gimp_selection_none($image);
  64.     # somebody seems to have changed the parameter for the following 3 lines
  65.     # omy office machine use the old variant, at home I need the new :(
  66.     plug_in_gauss_iir (1,$border,1,1,1);
  67.     plug_in_gauss_iir (1,$layer,2,1,1);
  68.     plug_in_bump_map (1,$layerpat,$layer,280,40,2,0,0,0,0,1,0,1);
  69.     #plug_in_gauss_iir (1,$image,$border,1,1,1);
  70.     #plug_in_gauss_iir (1,$image,$layer,2,1,1);
  71.     #plug_in_bump_map (1,$image,$layerpat,$layer,257,40,3,0,0,0,0,1,0,1);
  72.     # overlay border lines and random skew bricks
  73.     gimp_undo_push_group_start($image);
  74.     $h = 0; $j = 0; $wo = 0;
  75.     while ($h < $imgh)
  76.       {
  77.       $w = 0; $wo = 0; $wo = ($brickw / 2) if ($j == 1);
  78.       while ($w < $imgw)
  79.         {
  80.         $r = int(rand ($skew) - ($skew / 1));
  81.         if ($r != 0)
  82.           {
  83.           gimp_palette_set_foreground ("#ffffff") if ($r > 0); 
  84.           gimp_palette_set_foreground ("#000000") if ($r < 0); 
  85.           gimp_rect_select($image,$w+$wo+$bp,$h+$bp,$brickw,$brickh,2,0,0);
  86.           gimp_bucket_fill($layerpat,0,0,4*abs($r),0,0,1,1);
  87.           # halves
  88.           if (($j == 1) && ($w+$wo+$brickw > $imgw))
  89.             {
  90.             gimp_rect_select($image,0,$h+$bp,$brickw/2,$brickh,2,0,0);
  91.             gimp_bucket_fill($layerpat,0,0,4*abs($r),0,0,1,1);
  92.             }
  93.           }
  94.         $w += $brickw;
  95.         }
  96.       $j = 1 - $j;
  97.       $h += $brickh;
  98.       }
  99.     gimp_undo_push_group_end($image);
  100.     gimp_palette_set_background ($clr);
  101.     $layerb = gimp_image_add_new_layer($image,1,BG_IMAGE_FILL,0);
  102.     gimp_selection_all($image);
  103.     if ($bp ne "") 
  104.       {
  105.       gimp_patterns_set_pattern($bp);
  106.       gimp_bucket_fill($layerb,2,0,100,0,0,1,1); 
  107.       }
  108.     $border->edit_copy();
  109.     gimp_layer_add_alpha($layerb);
  110.     $mask = gimp_layer_create_mask($layerb,0);
  111.     $mask->edit_paste(0);
  112.     gimp_floating_sel_anchor(gimp_image_floating_selection($image));
  113.     gimp_image_add_layer_mask($image,$layerb,$mask);
  114.     gimp_selection_none($image);
  115.     gimp_image_remove_layer ($border);
  116.     gimp_image_remove_layer ($layer);
  117.     gimp_image_remove_layer_mask ($image,$layerb,0);
  118.     gimp_channel_ops_offset ($layerpat,1,0,-1,-1);
  119.     gimp_channel_ops_offset ($layerb,1,0,-1,-1);
  120.     $image;
  121. }
  122.  
  123. register
  124.         "do_bricks", 
  125.         "Generate brick texture",
  126.         "Try it out", 
  127.         "Tels", 
  128.         "http://bloodgate.com", 
  129.         "10/26/1999a",
  130.         N_"<Toolbox>/Xtns/Render/Bricks...", 
  131.         undef,
  132.         [ 
  133.          [PF_PATTERN,  "background", "Brick pattern", "Leather"],
  134.          [PF_STRING,  "highlight", "Second brick pattern for some highlightin (unused)", "unused yet"],
  135.          [PF_STRING,  "borderpattern", "Border pattern (empty for none)", ""],
  136.          [PF_COLOR,   "color", "Border color", "#dfdfdf"],
  137.          [PF_SPINNER,  "borderwidth", "Border width", 1, [0,1000,1]],
  138.          [PF_STRING,  "brickx", "Bricks in X", 8],
  139.          [PF_STRING,  "bricky", "Bricks in Y", 16],
  140.          [PF_STRING,  "imagew", "Width of image", 256],
  141.          [PF_STRING,  "imageh", "Height of image", 256],
  142.          [PF_STRING,  "skew", "Random darken/lighten factor (0..20)", 0]
  143.         ],
  144.         \&do_bricks;
  145.  
  146. exit main();
  147.  
  148.